package com.dd.ljzapplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.dd.ljzapplication.R;

import java.util.ArrayList;

/**
 * Created by LiJZ on 2016/10/18.
 */
public class HXVideoAdapter extends RecyclerView.Adapter {
	private ArrayList<String> listData;

	private Context mContext;

	/**
	 * 定义一个接口，处理recyclerview的item的点击事件
	 */
	public interface OnRVItemClickListener {
		/**
		 * @param v        该条目的view对象
		 * @param position 该位置对应的position
		 */
		void setItemClickListener(View v, int position);
	}

	/**
	 * 定义一个接口，处理recyclerview的item的长按事件
	 */
	public interface OnRVItemLongClickListener {
		/**
		 * @param v        该条目的view对象
		 * @param position 该位置对应的position
		 */
		void setItemLongClickListener(View v, int position);
	}


	/**
	 * item点击事件的接口
	 */
	private OnRVItemClickListener mRVItemClickListener;
	/**
	 * item长按事件的接口
	 */
	private OnRVItemLongClickListener mRVItemLongClickListener;

	//对外暴露的接口
	public void setOnRVItemClickListener(OnRVItemClickListener mRVItemClickListener) {
		this.mRVItemClickListener = mRVItemClickListener;
	}

	//对外暴露的接口
	public void setOnRVItemLongClickListener(OnRVItemLongClickListener rvItemLongClickListener) {
		this.mRVItemLongClickListener = rvItemLongClickListener;
	}


	public HXVideoAdapter(Context context) {
		this.mContext = context;
	}

	public void setDate(ArrayList<String> listData) {
		this.listData = listData;
		notifyDataSetChanged();
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(mContext).inflate(R.layout.item_hx_video, parent, false);
		MyViewHolder myViewHolder = new MyViewHolder(view);
		return myViewHolder;
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
		MyViewHolder myViewHolder = (MyViewHolder) holder;
		String path = listData.get(position);
//		myViewHolder.vv.setVideoPath(path);
		myViewHolder.iv.setImageBitmap(getVideoThumbnail(path));
		myViewHolder.tv.setText(listData.get(position));
		myViewHolder.iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mRVItemClickListener.setItemClickListener(v, position);
			}
		});
	}

	@Override
	public int getItemCount() {
		return listData == null || listData.size() == 0 ? 0 : listData.size();
	}


	static class MyViewHolder extends RecyclerView.ViewHolder {
		ImageView iv;
		TextView tv;
		VideoView vv;

		public MyViewHolder(View itemView) {
			super(itemView);
			iv = (ImageView) itemView.findViewById(R.id.iv);
			tv = (TextView) itemView.findViewById(R.id.tv);
			vv = (VideoView) itemView.findViewById(R.id.vv);
		}
	}

	public Bitmap getVideoThumbnail(String filePath) {
		Bitmap bitmap = null;
		MediaMetadataRetriever retriever = new MediaMetadataRetriever();
		try {
			retriever.setDataSource(filePath);
			bitmap = retriever.getFrameAtTime(0);
		}
		catch(IllegalArgumentException e) {
			e.printStackTrace();
		}
		catch (RuntimeException e) {
			e.printStackTrace();
		}
		finally {
			try {
				retriever.release();
			}
			catch (RuntimeException e) {
				e.printStackTrace();
			}
		}
		return bitmap;
	}
}
